package com.itheima.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.domain.po.Dept;
import com.itheima.domain.po.Emp;
import com.itheima.domain.po.EmpExpr;
import com.itheima.domain.query.EmpQueryParam;
import com.itheima.domain.vo.PageResult;
import com.itheima.mapper.DeptMapper;
import com.itheima.mapper.EmpExprMapper;
import com.itheima.mapper.EmpMapper;
import com.itheima.service.DeptService;
import com.itheima.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class EmpServiceImpl extends ServiceImpl<EmpMapper,Emp> implements EmpService {
    @Autowired
    DeptMapper deptMapper;
    @Autowired
    EmpExprMapper empExprMapper;

    @Override
    public PageResult<Emp> page(EmpQueryParam param) {
        Page<Emp> page = Page.of(param.getPage(), param.getPageSize());
        String name = param.getName();
        Integer gender = param.getGender();
        LocalDate begin = param.getBegin();
        LocalDate end = param.getEnd();
//        QueryWrapper<Emp> queryWrapper = Wrappers.<Emp>query()
//                .like(StringUtils.isNotBlank(name), "name", name)
//                .eq(gender != null,"gender",gender)
//                .between(begin != null && end != null,"entry_date",begin,end)
//                .orderByDesc("update_time");


//        LambdaQueryWrapper<Emp> lambdaQueryWrapper = Wrappers.<Emp>lambdaQuery()
//                .like(StringUtils.isNotBlank(name), Emp::getName, name)
//                .eq(gender != null, Emp::getGender, gender)
//                .between(begin != null && end != null, Emp::getEntryDate, begin, end)
//                .orderByDesc(Emp::getUpdateTime);
//        this.page(page,lambdaQueryWrapper);

        //LambdaQueryChainMapper =支持Lambda+将条件+目标方法串起来
//        this.lambdaQuery()
//                .like(StringUtils.isNotBlank(name), Emp::getName, name)
//                .eq(gender != null, Emp::getGender, gender)
//                .between(begin != null && end != null, Emp::getEntryDate, begin, end)
//                .orderByDesc(Emp::getUpdateTime).page(page);

        this.getBaseMapper().selectEmpPage(page,param);

        List<Emp> empList = page.getRecords();
        if (CollectionUtils.isNotEmpty(empList)) {
            empList.forEach(emp ->{
                Dept dept = deptMapper.selectById(emp.getDeptId());
                if (ObjectUtils.isNotNull(emp.getDeptId()) && ObjectUtils.isNotNull(dept)) {
                    emp.setDeptName(dept.getName());
                }
            });
        }
        
        return new PageResult<Emp>(page.getTotal(),empList);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void saveEmp(Emp emp) {
        emp.setPassword("123456");
        emp.setCreateTime(LocalDateTime.now());
        emp.setUpdateTime(LocalDateTime.now());
        this.save(emp);
        List<EmpExpr> exprList = emp.getExprList();
        if (CollectionUtils.isNotEmpty(exprList)) {
            exprList.forEach(expr->{
                expr.setEmpId(emp.getId());
            });
            empExprMapper.insert(exprList);
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(List<Integer> ids) {
        this.removeBatchByIds(ids);
        QueryWrapper<EmpExpr>
                queryWrapper = Wrappers.<EmpExpr>query().in("emp_id", ids);
        empExprMapper.delete(queryWrapper);
    }

    @Override
    public Emp selectById(Integer id) {
        Emp emp = this.getById(id);
        QueryWrapper<EmpExpr> queryWrapper = Wrappers.<EmpExpr>query().eq("emp_id", emp.getId());
        List<EmpExpr> empExprs = empExprMapper.selectList(queryWrapper);
        emp.setExprList(empExprs);
        return emp;
    }

    @Override
    public void updateEmp(Emp emp) {
        emp.setUpdateTime(LocalDateTime.now());
        this.updateById(emp);
        QueryWrapper<EmpExpr> queryWrapper = Wrappers.<EmpExpr>query().in("emp_id", emp.getId());
        empExprMapper.delete(queryWrapper);
        List<EmpExpr> exprList = emp.getExprList();
        if (CollectionUtils.isNotEmpty(exprList)) {
            exprList.forEach(expr -> {
                expr.setEmpId(emp.getId());
            });
            empExprMapper.insert(exprList);
        }
    }

}

